package com.lottery.ruok.lottrey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.MsgListAdapter;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetMsgRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetMsgResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 * 信息公告
 * */

public class InfoAnnouncementActivity extends BaseActivity {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.no_net_view)
    LinearLayout no_net_view;
//    @BindView(R.id.swip)
//    BGARefreshLayout swipeRefreshLayout;

    private MsgListAdapter msgListAdapter;
    private List<GetMsgResponse> listData = new ArrayList<>();
//    private int mIndex = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_announcement);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
//        BGARefreshViewHolder refreshViewHolder = new BGANormalRefreshViewHolder(this, true);
//        swipeRefreshLayout.setRefreshViewHolder(refreshViewHolder);
//
//        swipeRefreshLayout.setDelegate(new BGARefreshLayout.BGARefreshLayoutDelegate() {
//            @Override
//            public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
//                getList(mIndex);
//                return true;
//            }
//
//            @Override
//            public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
//                getList(0);
//            }
//        });

//        getList(0);

        getList();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(InfoAnnouncementActivity.this, InfoAnnouncementDetailActivity.class);
                intent.putExtra("title", listData.get(position).getTitle());
                intent.putExtra("create_time", listData.get(position).getCreate_time());
                intent.putExtra("content", listData.get(position).getContent());
                startActivity(intent);
            }
        });
    }

    private void getList() {
        RetrofitBase.getRetrofit().create(GetMsgRequest.class).getResult(UserInfo.getToken())
                .compose(RxHelper.<ArrayList<GetMsgResponse>>handleResult())
                .subscribe(new RxSubscribe<ArrayList<GetMsgResponse>>(this) {
                    @Override
                    protected void _onNext(ArrayList<GetMsgResponse> getMsgResponses) {
//                        if(msgListAdapter == null){

//                        } else {
//                            msgListAdapter.notifyDataSetChanged();
//                        }

                        if (getMsgResponses.size() > 0) {
                            no_net_view.setVisibility(View.GONE);
                            listView.setVisibility(View.VISIBLE);
                            listData.clear();
                            listData.addAll(getMsgResponses);
                            msgListAdapter = new MsgListAdapter(getMsgResponses, InfoAnnouncementActivity.this);
                            listView.setAdapter(msgListAdapter);
                        } else {
                            no_net_view.setVisibility(View.VISIBLE);
                            listView.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        no_net_view.setVisibility(View.VISIBLE);
                        listView.setVisibility(View.GONE);
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }

//    private void getList(final int index) {
//        RetrofitBase.getRetrofit().create(GetMsgRequest.class).getResult(index, 10)
//                .compose(RxHelper.<ArrayList<GetMsgResponse>>handleResult())
//                .subscribe(new RxSubscribe<ArrayList<GetMsgResponse>>(this) {
//                    @Override
//                    protected void _onNext(ArrayList<GetMsgResponse> getMsgResponses) {
//                        mIndex = index + 10;
//
//                        if(index == 0){
//                            msgListAdapter = new MsgListAdapter(getMsgResponses, InfoAnnouncementActivity.this);
//                            listView.setAdapter(msgListAdapter);
//                            swipeRefreshLayout.endRefreshing();
//                        } else {
//                            msgListAdapter.getGetMsgResponses().addAll(getMsgResponses);
//                            msgListAdapter.notifyDataSetChanged();
//                            swipeRefreshLayout.endLoadingMore();
//                        }
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        super.onError(e);
//
//                        if(index == 0){
//                            swipeRefreshLayout.endRefreshing();
//                        } else {
//                            swipeRefreshLayout.endLoadingMore();
//                        }
//                    }
//
//                    @Override
//                    protected boolean showDialog() {
//                        return false;
//                    }
//                });
//    }
}
